/*
 * @LastEditTime: 2023-11-06 10:09:55
 * @FilePath: \bdcar-net\src\utils\dict.js
 
 */
import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data'

/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType, index) => {
      setDataByDictType(res.value, dictType);
    })
    return toRefs(res.value);
  })()
}

/**
 * 根据type获取字典数据，如果传递key字段则用key去接受获得的数据
 */
export function setDataByDictType(data, dictType, key) {
  let dictKey = key ? key : dictType
  data[dictKey] = [];
  const dicts = useDictStore().getDict(dictType);
  if (dicts) {
    data[dictKey] = dicts;
  } else {
    getDicts(dictType).then(resp => {
      data[dictKey] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
      useDictStore().setDict(dictType, data[dictKey]);
    })
  }
}